package com.lixin.smartclassroom.ui.home.tab1.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.api.addComment
import com.lixin.smartclassroom.api.addLikeComment
import com.lixin.smartclassroom.api.coursedetail
import com.lixin.smartclassroom.api.coursepllist
import com.lixin.smartclassroom.bean.BaseBean
import com.lixin.smartclassroom.bean.DataListBean
import com.lixin.smartclassroom.common.saveSuccessDialog
import com.lixin.smartclassroom.common.shareDialog
import com.lixin.smartclassroom.databinding.ActivityCourseDetailBinding
import com.lixin.smartclassroom.ui.home.adapter.comment
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.dp2px
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.gone
import com.lixinkeji.extension.load
import com.lixinkeji.extension.loadCircle
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.setRoundRectBg
import com.lixinkeji.extension.start
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.extension.visible

/**
 * Description 课程详情
 * Author: lwc
 * Date:2023/12/6 11:16
 */
class CourseDetailAct : BaseActivity<ActivityCourseDetailBinding>() {
    override fun getViewBinding() = ActivityCourseDetailBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    private lateinit var listBean: ArrayList<DataListBean>
    override fun title() = "课程详情"
    private var courseId = ""
    private var coursetitle = ""
    private lateinit var baseBean: BaseBean
    private var isPlaying = false

    @SuppressLint("SetTextI18n")
    override fun iniView() {
        courseId = getIntentString("courseid")
        binding.apply {
            etComment.setRoundRectBg(Color.parseColor("#F3F3F3"), dp2px(20f).toInt())
            tvDescribe.setRoundRectBg(Color.parseColor("#F3F3F3"), dp2px(7f).toInt())
            tvType.setRoundRectBg(Color.parseColor("#FF7073"), dp2px(5f).toInt())


            scopeNetLife {
                baseBean = coursedetail(courseId)
                binding.apply {
                    tvTitle.text ="                  "+ baseBean.title
                    coursetitle = baseBean.title!!
                    tvType.text = baseBean.biaoshi
                    if (baseBean.teacherimage != null)
                        img.loadCircle(baseBean.teacherimage!!)
                    tvName.text = baseBean.teachername
                    tvTime.text = baseBean.createdate
                    tvLookNum.text = baseBean.seecount + "人浏览"
                    tvComment.text = baseBean.plcount
                    tvLike.text = baseBean.clickcount
                    tvLike.isSelected = baseBean.ifclick == "1"
                    tvDescribe.setText(
                        baseBean.content
                    )
                    tvDescribe.setMaxLine(4)
                    tvCommentCount.text = "精彩评论(" + baseBean.plcount + ")"

                    if (baseBean.url!!.isNotEmpty()) {
                        if (baseBean.url!!.contains("mp4")) {
                            videoPlayer.visible()
                            videoPlayer.setUp(baseBean.url, true, "")
                            //增加封面
                            val imageView = ImageView(this@CourseDetailAct)
                            imageView.scaleType = ImageView.ScaleType.CENTER_CROP
                            Glide.with(this@CourseDetailAct).load(baseBean.url).into(imageView)
                            videoPlayer.thumbImageView = imageView
                            videoPlayer.backButton.gone()
                            videoPlayer.fullscreenButton.visible()
                            //不需要屏幕旋转
                            videoPlayer.isNeedOrientationUtils = false
                        } else {
                            courseImage.visible()
                            videoPlayer.gone()
                            courseImage.load(baseBean.url!!)
                        }
                    } else {
                        videoPlayer.gone()
                    }

                }

                listBean = coursepllist(courseId)!!
                binding.recyclerComment.comment(1).models = listBean
            }

        }

    }


    override fun onClick() {
        binding.apply {
            btnShare.onClick {
                shareDialog(courseId, coursetitle)
            }
            btnFull.onClick {
                start(MediaPlayActivity::class.java) {
                    putExtra("video_url", baseBean.url)
                }
            }
            tvLike.onClick {
                scopeNetLife {
                    val resultBean = addLikeComment(courseid = courseId)
                    if (resultBean.result == "0") {
                        if (tvLike.isSelected) {
                            tvLike.text = (baseBean.clickcount?.toInt()?.minus(1)).toString()
                            baseBean.clickcount =
                                (baseBean.clickcount?.toInt()?.minus(1)).toString()
                        } else {
                            tvLike.text = (baseBean.clickcount?.toInt()?.plus(1)).toString()
                            baseBean.clickcount = (baseBean.clickcount?.toInt()?.plus(1)).toString()
                        }
                        tvLike.isSelected = !tvLike.isSelected

                    }
                }
            }
            tvSend.onClick {
                if (etComment.text().isEmpty()) {
                    toast("请输入评论内容")
                    return@onClick
                }
                scopeNetLife {
                    addComment(courseId, etComment.text())
//                    baseBean.plcount = (baseBean.plcount?.toInt()?.plus(1)).toString()
//                    tvCommentCount.text = "精彩评论(" + baseBean.plcount + ")"
//                    listBean.clear()
//                    listBean.addAll(coursepllist(courseId)!!)
//                    recyclerComment.bindingAdapter.notifyDataSetChanged()
                    etComment.setText("")
                    saveSuccessDialog("评论成功，等待审核")
                }
            }
            videoPlayer.onClick {
                if (isPlaying)
                    videoPlayer.onVideoPause()
                else
                    videoPlayer.startPlayLogic()
            }
            videoPlayer.fullscreenButton.onClick {
                start(MediaPlayActivity::class.java) {
                    putExtra("video_url", baseBean.url)
                }
            }
        }
    }

}